<?php
namespace app\backstage\logic;
use think\Db;
class OrderSummary
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'create_date,money,ordinary_recharge_money,activity_recharge_money,non_vip_money,non_vip_user_num';
        $field .= ',non_vip_paid_num,non_vip_unpaid_num,vip_money,vip_user_num,vip_paid_num,vip_unpaid_num,new_zc_money';

        $groupBy = '';
        $type = 3;
        $where = [];
        $uid = request()->_user->id;

        if(isset($params['channel_id']) && $params['channel_id'] >= 1)
        {
            $type = 2;
            $where[] = ['channel_id','=',$params['channel_id']];
        }else{
            if (request()->_role->alias == 'agent') {
                $type = 1;
                $where[] = ['agent_id','=',$uid];
            }

            if (request()->_role->alias == 'channel') {
                $type = 2;
                $where[] = ['channel_id','=',$uid];
            }
        }

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['create_date' => 'DESC'];
        }

        $OrderSummary = \think\facade\App::model('OrderSummary');
        $list = $OrderSummary->getList($type, $where, $field, $params['page'], $params['limit'], $orderBy, $groupBy);

        if ($list) {

            foreach ($list as $k => $v) {
                $list[$k]['money'] = $v['money'] > 0 ? round($v['money']/100,2) : 0;
                $list[$k]['ordinary_recharge_money'] = $v['ordinary_recharge_money'] > 0 ? round($v['ordinary_recharge_money']/100,2) : 0;
                $list[$k]['activity_recharge_money'] = $v['activity_recharge_money'] > 0 ? round($v['activity_recharge_money']/100,2) : 0;
                $list[$k]['non_vip_money'] = $v['non_vip_money'] > 0 ? round($v['non_vip_money']/100,2) : 0;
                $list[$k]['vip_money'] = $v['vip_money'] > 0 ? round($v['vip_money']/100,2) : 0;
                $list[$k]['non_vip_money_avg'] = $v['non_vip_money'] > 0 ? sprintf("%.3f",$v['non_vip_money']/$v['non_vip_user_num']/100) : 0;
                $list[$k]['non_vip_rate'] = $v['non_vip_paid_num'] > 0 ? round(($v['non_vip_paid_num']/($v['non_vip_paid_num']+$v['non_vip_unpaid_num']))*100,2) : 0;
                $list[$k]['vip_money_avg'] = $v['vip_money'] > 0 ? sprintf("%.3f",$v['vip_money']/$v['vip_user_num']/100) : 0;
                $list[$k]['vip_rate'] = $v['vip_paid_num'] > 0 ? round(($v['vip_paid_num']/($v['vip_paid_num']+$v['vip_unpaid_num']))*100,2) : 0;
                $list[$k]['new_zc_money'] = $v['new_zc_money'] > 0 ? round($v['new_zc_money']/100,2) : 0;
            }
        }

        $result['count'] = $OrderSummary->getTotalNumber($type, $where, $groupBy);
        $result['list']  = $list;
        $result['summary']['today'] = $this->getSummaryToday($params);
        $result['summary']['yesterday'] = $this->getSummary($params,'yesterday');
        $result['summary']['month'] = $this->getSummary($params,'month');
        $result['summary']['total'] = $this->getSummary($params,'total');

        return $result;
    }

    /*今日数据汇总*/
    private function getSummaryToday($params)
    {
        $today = date("Y-m-d");
        $groupBy = '';
        $where = [];
        $uid = request()->_user->id;

        if(isset($params['channel_id']) && $params['channel_id'] >= 1)
        {
            $where[] = ['channel_id','=',$params['channel_id']];
        }else{
            if (request()->_role->alias == 'agent') {
                $where[] = ['agent_id','=',$uid];
            }

            if (request()->_role->alias == 'channel') {
                $where[] = ['channel_id','=',$uid];
            }
        }

        $where[] = ['create_date','=',$today];

        $money = Db::table('pay_order_success')->where($where)->where('status',2)->sum('total_fee');
        $non_vip_money = Db::table('pay_order_success')->where($where)->where('status',2)->where('type',1)->sum('total_fee');
        $vip_money = Db::table('pay_order_success')->where($where)->where('status',2)->where('type',2)->sum('total_fee');
        $ordinary_recharge_money = Db::table('pay_order_success')->where('status',2)->where($where)->where('recharge_type',1)->sum('total_fee');
        $activity_recharge_money = Db::table('pay_order_success')->where('status',2)->where($where)->where('recharge_type',2)->sum('total_fee');
        $vip_paid_num = Db::table('pay_order_success')->where($where)->where('type',2)->where('status',2)->count('id');
        $vip_unpaid_num = Db::table('pay_order')->where($where)->where('type',2)->where('status','<>',2)->count('id');
        $non_vip_paid_num = Db::table('pay_order_success')->where($where)->where('type',1)->where('status',2)->count('id');
        $non_vip_unpaid_num = Db::table('pay_order')->where($where)->where('type',1)->where('status','<>',2)->count('id');
        $new_zc_money = Db::table('pay_order_success')->where($where)->where('status', 2)->where("user_reg_date", '=', $today)->sum('total_fee');

        $summary['money']                   = getQuotient($money,100);
        $summary['non_vip_money']           = getQuotient($non_vip_money,100);
        $summary['vip_money']               = getQuotient($vip_money,100);
        $summary['ordinary_recharge_money'] = getQuotient($ordinary_recharge_money,100);
        $summary['activity_recharge_money'] = getQuotient($activity_recharge_money,100);
        $summary['vip_paid_num']            = getFinalIntValue($vip_paid_num);
        $summary['vip_unpaid_num']          = getFinalIntValue($vip_unpaid_num);
        $summary['vip_rate']                = getQuotient($summary['vip_paid_num'],($summary['vip_paid_num']+$summary['vip_unpaid_num']),2,'m',100);
        $summary['non_vip_paid_num']        = getFinalIntValue($non_vip_paid_num);
        $summary['non_vip_unpaid_num']      = getFinalIntValue($non_vip_unpaid_num);
        $summary['non_vip_rate']            = getQuotient($summary['non_vip_paid_num'],($summary['non_vip_paid_num']+$summary['non_vip_unpaid_num']),2,'m',100);
        $summary['new_zc_money']            = getQuotient($new_zc_money,100);

        return $summary;
    }

    /*汇总数据获取*/
    private function getSummary($params,$type_s)
    {
        $groupBy = '';
        $type = 3;
        $where = [];
        $uid = request()->_user->id;

        if(isset($params['channel_id']) && $params['channel_id'] >= 1)
        {
            $type = 2;
            $where[] = ['channel_id','=',$params['channel_id']];
        }else{
            if (request()->_role->alias == 'agent') {
                $type = 1;
                $where[] = ['agent_id','=',$uid];
            }

            if (request()->_role->alias == 'channel') {
                $type = 2;
                $where[] = ['channel_id','=',$uid];
            }
        }

        $field = 'create_date,money,ordinary_recharge_money,activity_recharge_money,non_vip_money,non_vip_user_num';
        $field .= ',non_vip_paid_num,non_vip_unpaid_num,vip_money,vip_user_num,vip_paid_num,vip_unpaid_num';
        $field .= ',new_zc_money';

        if($type_s == 'today')
        {
            $where[] = ['create_date','=',date('Y-m-d')];
        }
        if($type_s == 'yesterday')
        {
            $where[] = ['create_date','=',date('Y-m-d', strtotime('-1days'))];
        }
        if($type_s == 'month')
        {
            $start_date = date('Y-m-01',time());
            $end_date = date('Y-m-t',time());
            $where[] = ['create_date','>=',$start_date];
            $where[] = ['create_date','<=',$end_date];
        }

        if($type_s == 'month' || $type_s == 'total')
        {
            $field = 'SUM(money) AS money,SUM(ordinary_recharge_money) AS ordinary_recharge_money,';
            $field .= 'SUM(activity_recharge_money) AS activity_recharge_money,SUM(non_vip_money) AS non_vip_money,';
            $field .= 'SUM(non_vip_user_num) AS non_vip_user_num,SUM(non_vip_paid_num) AS non_vip_paid_num,';
            $field .= 'SUM(non_vip_unpaid_num) AS non_vip_unpaid_num,SUM(vip_money) AS vip_money,';
            $field .= 'SUM(vip_user_num) AS vip_user_num,SUM(vip_paid_num) AS vip_paid_num,SUM(vip_unpaid_num) AS vip_unpaid_num';
        }

        $OrderSummary = \think\facade\App::model('OrderSummary');
        $record = $OrderSummary->getOne($type, $where, $field, $groupBy);

        $summary['money'] = $record && $record['money'] > 0 ? round($record['money']/100,2) : 0;
        $summary['non_vip_money'] = $record && $record['non_vip_money'] > 0 ? round($record['non_vip_money']/100,2) : 0;
        $summary['vip_money'] = $record && $record['vip_money'] > 0 ? round($record['vip_money']/100,2) : 0;
        $summary['ordinary_recharge_money'] = $record && $record['ordinary_recharge_money'] > 0 ? round($record['ordinary_recharge_money']/100,2) : 0;
        $summary['activity_recharge_money'] = $record && $record['activity_recharge_money'] > 0 ? round($record['activity_recharge_money']/100,2) : 0;
        $summary['vip_paid_num'] = $record && $record['vip_paid_num'] > 0 ? $record['vip_paid_num'] : 0;
        $summary['vip_unpaid_num'] = $record && $record['vip_unpaid_num'] > 0 ? $record['vip_unpaid_num'] : 0;
        $summary['vip_rate'] = $record && $record['vip_paid_num'] > 0 ? round(($record['vip_paid_num']/($record['vip_paid_num']+$record['vip_unpaid_num']))*100,2) : 0;
        $summary['non_vip_paid_num'] = $record && $record['non_vip_paid_num'] > 0 ? $record['non_vip_paid_num'] : 0;
        $summary['non_vip_unpaid_num'] = $record && $record['non_vip_unpaid_num'] > 0 ? $record['non_vip_unpaid_num'] : 0;
        $summary['non_vip_rate'] = $record && $record['non_vip_paid_num'] > 0 ? round(($record['non_vip_paid_num']/($record['non_vip_paid_num']+$record['non_vip_unpaid_num']))*100,2) : 0;
        if($type_s == 'yesterday')
        {
            $summary['new_zc_money'] =  $record && $record['new_zc_money'] > 0 ? round($record['new_zc_money']/100,2) : 0;
            $yesWhere = [
                ['create_time', '>=', date('Y-m-d', strtotime('-1days')). ' 00:00:00'],
                ['create_time', '<=', date('Y-m-d', strtotime('-1days')).' '.date('H:i:s')],
            ];
            $summary['yesterday_money'] = round(Db::table('pay_order_success')->where(array_merge($where, $yesWhere))->sum('total_fee') / 100,2);
        }
        return $summary;
    }
}